﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.CodeManage {
      public class LeadCodeService {
	   public BaseRepository _baseRepository = new BaseRepository();
	   /// <summary>
	   /// 分页显示
	   /// </summary>
	   /// <param name="page"></param>
	   /// <param name="Psearch"></param>
	   /// <returns></returns>
	   public PagedList<CodeManage_GenCode>GetLeadCodePageListService(ref PagerModel page, string Lsearch) {

		   page.FldName = @" * ";
           page.TblName = @"(select cg.ID,cg.CodeType,cg.PatchId,cg.CodeTypeText,oe.ID EnterpriseId,cg.ViewDel,cg.ChangeDate,
                   cg.Gen_Code_Nums,cg.Image,cg.Query_Result_Content,cg.Query_Tel_Content,cg.Query_Message_Content,
                   cg.Status  ,cgc.Batch,cg.Sequence,cgc.ViewFlag,
                   oe.EnterpriseName,oe.Code from CodeManage_GenCode cg left join Organization_Enterprise oe
                   on cg.EnterpriseId=oe.ID
                   left join CodeManage_GenCodeBatch cgc on cg.PatchId=cgc.ID
                   where cg.ViewDel=0
                ) w";
		 
		   if (string.IsNullOrEmpty(page.FldSort) || page.FldSort == "")
			   page.FldSort = "  ID desc ";
		   MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref page);
		   IEnumerable<CodeManage_GenCode> resultList = _baseRepository.ExecuteStoredProcedureCollection<CodeManage_GenCode>("Common_PageList", parameters);
		   return new PagedList<CodeManage_GenCode>(resultList, page.Page, page.PageSize, 1, Convert.ToInt32(parameters[6].Value));

	   }
	   /// <summary>
	   /// 删除
	   /// </summary>
	   /// <param name="id"></param>
	   /// <returns></returns>
	   public int DelLeadCodeService(int id) {
		   CodeManage_GenCode cg = new CodeManage_GenCode();
		   cg.ViewDel = 1;	  
		   cg.ID = id;
		   cg.ChangeDate = DateTime.Now;
		   return _baseRepository.Update<CodeManage_GenCode>(cg);
	   }
		 /// <summary>
		/// 启用禁用
		/// </summary>
		/// <param name="flge"></param>
		/// <param name="CheckValue"></param>
	   /// <returns></returns>
	   public int GetViewFlagsService(int flge, string[] CheckValue) 
	   {
		   string sql;
		   int sum = 0;
		   for (int i = 0; i < CheckValue.Length - 1; i++) {
			   sql = string.Format("UPDATE codemanage_gencodebatch SET ViewFlag={0} WHERE ID=( SELECT PatchId FROM  codemanage_gencode WHERE ID={1})", flge, CheckValue[i]);
			   sum += _baseRepository.ExecuteSql<int>(sql);
		   }
		   return sum;
	   }
	   public CodeManage_GenCode GetLeadModelId(int id) {
		   string sql = @"SELECT g.ID,EnterpriseName,Batch,Gen_Code_Nums,LabelImage,Query_Result_Content,Query_Message_Content,Status,CodeType,Code,g.ChangeDate,g.ViewDel FROM
	                                         codemanage_gencode g
                                             LEFT JOIN codemanage_gencodebatch c ON g.PatchId = c.ID
                                             LEFT JOIN organization_enterprise e on c.EnterpriseId=e.ID where  g.ID="+id;
		   var result = _baseRepository.ExecuteSqlCollection<CodeManage_GenCode>(sql).FirstOrDefault();
		   return result;
       }

       #region 获取当个Model
       public CodeManage_GenCode GetModel_ID(int id) 
       {
           string sql = " select * from codemanage_gencode where ID="+id;
           return _baseRepository.ExecuteSqlCollection<CodeManage_GenCode>(sql).FirstOrDefault();
       }
       #endregion

       #region 导码
       /// <summary>
       /// 修改表AntiFake_Digitcode 中的WebFileUrl字段值
       /// batchId  批次编号
       /// enterId  机构编号
       /// </summary>
       /// <returns></returns>
       public int addWebFileUrl(string batchId, string enterId, string fileUrl, string flag, string codeType)
       {
           batchId = batchId.PadLeft(2, '0');
           enterId = enterId.PadLeft(4, '0');
           string sql = @"update AntiFake_Digitcode{0} set Flag='{1}';";
           string sql2 = @"delete from CodeManage_Temp where EnterpriseCode={0} and Batch={1};
                    insert into CodeManage_Temp(EnterpriseCode,Batch,WebFileUrl,CodeType,Flag) values('{2}','{3}','{4}','{5}','6')";
           sql = string.Format(sql, enterId + batchId, flag);
           sql2 = string.Format(sql2, enterId, batchId, enterId, batchId, fileUrl, codeType);
         _baseRepository.ExecuteSql<int>(sql, "Data Source=101.37.76.4;port=3306;Initial Catalog=nbcodemain;userid=root;password=root001",null);
         int result = _baseRepository.ExecuteSql<int>(sql2);
           return result;
       }

          #endregion
      }
  }

